import { Injectable } from '@angular/core';
import { Router } from '@angular/router';

import { UserService } from './user.service';

@Injectable()
export class AuthService {
  loggedIn = false;
  currentUser = {};

  constructor(private userService: UserService,
              private router: Router) { }

  login(emailAndPassword) {
    return this.userService.login(emailAndPassword).map(res => res).map(
      res => {
        if (res.code === '1000' ) {
          localStorage.setItem('token', res.data.token);
          this.loggedIn = true;
        }
        return res;
      }
    );
  }

  logout() {
    localStorage.removeItem('token');
    this.loggedIn = false;
    this.router.navigate(['/login']);
  }
}
